package com.moji.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moji.entity.MjAboutUs;
import com.moji.entity.MjHomeProduct;
import com.moji.entity.MjHomeSolution;
import com.moji.entity.MjSolution;
import com.moji.mapper.MjAboutUsMapper;
import com.moji.mapper.MjHomeSolutionMapper;
import com.moji.mapper.MjSolutionMapper;
import com.moji.service.MjAboutUsService;
import com.moji.service.MjHomeSolutionService;
import io.swagger.annotations.ApiModelProperty;
import org.apache.http.util.TextUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 首页解决方案详情(MjHomeSolution)表服务实现类
 *
 * @author makejava
 * @since 2024-07-21 14:36:20
 */
@Service("mjHomeSolutionService")
public class MjHomeSolutionServiceImpl extends ServiceImpl<MjHomeSolutionMapper, MjHomeSolution> implements MjHomeSolutionService {
    @Resource
    private MjHomeSolutionMapper mjHomeSolutionDao;
    @Resource
    private MjSolutionMapper mjSolutionMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public MjHomeSolution queryById(Long id) {
        return this.mjHomeSolutionDao.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param mjHomeSolution 筛选条件
     * @return 查询结果
     */
    @Override
    public List<MjHomeSolution> queryByPage(MjHomeSolution mjHomeSolution) {
        if (mjHomeSolution.getSolutionId() == null) {
            if (mjHomeSolution.getRouter() == null || TextUtils.isEmpty(mjHomeSolution.getRouter())) {
                return null;
            }
            LambdaQueryWrapper<MjSolution> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(MjSolution::getRouter, mjHomeSolution.getRouter());
            queryWrapper1.orderByAsc(MjSolution::getSort);
            queryWrapper1.orderByDesc(MjSolution::getUpdateTime);
            List<MjSolution> mjSolutions = mjSolutionMapper.selectList(queryWrapper1);
            if (mjSolutions == null || mjSolutions.size() == 0) {
                return null;
            }
            mjHomeSolution.setSolutionId(mjSolutions.get(0).getId());
        }
        LambdaQueryWrapper<MjHomeSolution> queryWrapper = new LambdaQueryWrapper<>();
        if (mjHomeSolution.getSolutionId() != null) {
            queryWrapper.eq(MjHomeSolution::getSolutionId, mjHomeSolution.getSolutionId());
        }
        if (mjHomeSolution.getType() != null) {
            queryWrapper.eq(MjHomeSolution::getType, mjHomeSolution.getType());
        }
        queryWrapper.orderByAsc(MjHomeSolution::getSort);
        queryWrapper.orderByDesc(MjHomeSolution::getUpdateTime);
        List<MjHomeSolution> page = this.list(queryWrapper);
        return page;
    }

    /**
     * 新增数据
     *
     * @param mjHomeSolution 实例对象
     * @return 实例对象
     */
    @Override
    public MjHomeSolution insert(MjHomeSolution mjHomeSolution) {
        mjHomeSolution.setCreateTime(new Date());
        mjHomeSolution.setUpdateTime(new Date());
        this.mjHomeSolutionDao.insert(mjHomeSolution);
        return mjHomeSolution;
    }

    /**
     * 修改数据
     *
     * @param mjHomeSolution 实例对象
     * @return 实例对象
     */
    @Override
    public MjHomeSolution update(MjHomeSolution mjHomeSolution) {
        mjHomeSolution.setUpdateTime(new Date());
        LambdaQueryWrapper<MjHomeSolution> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(MjHomeSolution::getId, mjHomeSolution.getId());
        this.mjHomeSolutionDao.update(mjHomeSolution, queryWrapper);
        return this.queryById(mjHomeSolution.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.mjHomeSolutionDao.deleteById(id) > 0;
    }
}
